﻿using AutoMapper;
using MediatR;
using MyMES.Domain.W.Liststructure;
using MyMES.Error;
using MyMES.Infrastructure.Interface;
using MyMES.Writer.API.Application.Commends.W.realize;

namespace MyMES.Writer.API.Application.CommendHandlers.W.realize
{
    public class UpdateSupplierCommandHanders : IRequestHandler<UpdateSupplierCommands, APIResult<Supplier>>
    {
        private readonly IBaseRepository<Supplier> supplierRepository;
        private readonly IMapper mapper;

        public UpdateSupplierCommandHanders(IBaseRepository<Supplier> supplierRepository, IMapper mapper)
        {
            this.supplierRepository = supplierRepository;
            this.mapper = mapper;
        }


        /// <summary>
        /// 修改供应商信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<APIResult<Supplier>> Handle(UpdateSupplierCommands request, CancellationToken cancellationToken)
        {
            APIResult<Supplier> result = new APIResult<Supplier>();
            result.Code = APIEnum.修改成功;
            result.Msg = "修改成功";
            var supplier = mapper.Map<Supplier>(request);
            var list = $"Update Supplier set SupplierNumber='{request.SupplierNumber}',SupplierName='{request.SupplierName}',SupplierAbbreviation='{request.SupplierAbbreviation}',Sid='{request.Sid}',SupplierRating='{request.SupplierRating}',SupplierPhone='{request.SupplierPhone}',IsEnable=@IsEnable,Remark='{request.Remark}',SupplierEnglishName='{request.SupplierEnglishName}',SupplierProfile='{request.SupplierProfile}',SupplierAddress='{request.SupplierAddress}',SupplierOfficialAddress='{request.SupplierOfficialAddress}',SupplierEmailAddress='{request.SupplierEmailAddress}',UnifyCode='{request.UnifyCode}',SupplierLOGOAddress='{request.SupplierLOGOAddress}',IsDelete=@IsDelete where SupplierId='{request.SupplierId}'";
            await supplierRepository.Update(supplier, list);
            return result;
        }
    }
}
